package com.sgb.kjwl.view.ui.activity.packers;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.core.util.Pair;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;

import com.bumptech.glide.Glide;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityFindPeopleBinding;
import com.sgb.kjwl.model.entity.FindPeopleEntity;
import com.sgb.kjwl.viewmodel.packers.PackersEngineeringTenderModel;
import com.sgb.link.entity.PickFilterEntity;
import com.sgb.kjwl.model.entity.packers.PickResultEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.view.ui.adapter.packers.FindPeopleAdapter;
import com.sgb.kjwl.view.widget.PickPopWindow1;
import com.sgb.kjwl.view.widget.ThreeLevelAreaWindow;
import com.sgb.kjwl.view.widget.ThreeLevelFilterWindow;
import com.sgb.link.view.widget.XLinearLayoutManager;
import com.sgb.kjwl.viewmodel.packers.PackersFindPeopleModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.decoration.RecyclerCustomDecoration;
import com.swgk.core.recyclerview.entity.BaseAreaEntity;
import com.swgk.core.util.PickSelectEntity;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.List;

/**
 * description:找工人列表
 * @author liWei
 * @date 2020/12/19
 */
public class PackersFindPeopleActivity extends AppActivity {

    PackersFindPeopleModel viewModel;
    private ActivityFindPeopleBinding binding;
    private FindPeopleAdapter findPeopleAdapter;

    /**
     * 工种
     */
    private ThreeLevelFilterWindow popWorkType;
    private PickResultEntity pickResultIndustry;
    private PickResultEntity pickResultCategory;
    private PickResultEntity pickResultWorkType;
    /**
     * 专业职级
     */
    private PickPopWindow1 popWorkerLevel;
    private PickResultEntity pickResultWorkerLevel;
    /**
     * 工龄
     */
    private PickPopWindow1 popWorkYear;
    private PickResultEntity pickResultWorkYear;
    /**
     * 所在区域
     */
    private ThreeLevelAreaWindow popCity;
    private PickResultEntity pickResultProvince;
    private PickResultEntity pickResultCity;
    private PickResultEntity pickResultArea;
    private AddressEntity mAddress;
    private String time;

    public static void start(Context context,String title) {
        Intent intent = new Intent();
        intent.putExtra("title",title);
        intent.setClass(context, PackersFindPeopleActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
//        DaggerPackersComponent.builder().
//                appComponent(appComponent)
//                .packersModule(new PackersModule(this))
//                .build()
//                .inject(this);
    }

    @Override
    protected boolean statusBarDark() {
        return false;
    }

    private void initFilterData(boolean isLoadMore) {
        viewModel.getNewList(pickResultIndustry.getNo(), pickResultCategory.getNo(), pickResultWorkType.getNo(),
                pickResultWorkerLevel.getNo(), pickResultWorkYear.getNo(), pickResultProvince.getNo(),
                pickResultCity.getNo(), pickResultArea.getNo(), isLoadMore,time);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActionBar.setTitle(getIntent().getStringExtra("title"));
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_find_people);
        viewModel = new ViewModelProvider(this).get(PackersFindPeopleModel.class);
        if (findPeopleAdapter == null) {
            findPeopleAdapter = new FindPeopleAdapter(this, null, Glide.with(this));
        }
        findPeopleAdapter.setOnItemClick(new AdapterOnItemClick<FindPeopleEntity>() {
            @Override
            public void onItemClick(FindPeopleEntity findPeopleEntity, int position) {
                FindPeopleDetailActivity.start(PackersFindPeopleActivity.this, findPeopleEntity.getNo());
            }
        });

        binding.recyclerView.setLayoutManager(new XLinearLayoutManager(getActivity()));
        binding.recyclerView.addItemDecoration(new RecyclerCustomDecoration(0,
                0,
                0,
                getResources().getDimension(R.dimen.dp5)));
        binding.recyclerView.setAdapter(findPeopleAdapter);
        binding.recyclerView.setLoadingMoreEnabled(true);
        binding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                initFilterData(false);
            }

            @Override
            public void onLoadMore() {
                initFilterData(true);
            }
        });

        pickResultIndustry = new PickResultEntity();
        pickResultCategory = new PickResultEntity();
        pickResultWorkType = new PickResultEntity();
        pickResultWorkerLevel = new PickResultEntity();
        pickResultWorkYear = new PickResultEntity();
        pickResultProvince = new PickResultEntity();
        pickResultCity = new PickResultEntity();
        pickResultArea = new PickResultEntity();

        mAddress = (AddressEntity) getIntent().getSerializableExtra("entity");
        if (mAddress != null){
            pickResultIndustry.setNo((mAddress).getNo());
            pickResultIndustry.setName((mAddress).getName());
        }

        String proficiency = getIntent().getStringExtra("proficiency");
        String workYear = getIntent().getStringExtra("workYear");
        String provinceNo = getIntent().getStringExtra("provinceNo");
        String industryNo = getIntent().getStringExtra("industryNo");
        String categoryNo = getIntent().getStringExtra("categoryNo");
        String workNo = getIntent().getStringExtra("workNo");
        String cityNo = getIntent().getStringExtra("cityNo");
        String cityName = getIntent().getStringExtra("cityName");
        String workName = getIntent().getStringExtra("workName");
        String workYearName = getIntent().getStringExtra("workYearName");
        String professionalLevelName = getIntent().getStringExtra("professionalLevelName");
        time = getIntent().getStringExtra("time");

        if (!TextUtils.isEmpty(proficiency)){
            pickResultWorkerLevel.setNo(proficiency);
        }

        if (!TextUtils.isEmpty(workYear)){
            pickResultWorkYear.setNo(workYear);
        }

        if (!TextUtils.isEmpty(provinceNo)){
            pickResultProvince.setNo(provinceNo);
            pickResultCity.setNo(cityNo);
            pickResultCity.setName(cityName);
        }

        if (!TextUtils.isEmpty(industryNo)){
            pickResultIndustry.setNo(industryNo);
            pickResultCategory.setNo(categoryNo);
            pickResultWorkType.setNo(workNo);

            String[] workNameArray = workName.split("/");
        }
    }

    @Override
    protected void init() {
        initFilterData(false);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.tv_work_type) {
            if (popWorkType == null) {
                viewModel.getLinkAgeWorkType();
            } else {
                showWorkTypePop(null);
            }
        } else if (id == R.id.tv_worker_level) {
            if (popWorkerLevel == null) {
                viewModel.getWorkerLevel();
            } else {
                shoWorkerLevelPop(null);
            }
        } else if (id == R.id.tv_work_year) {
            if (popWorkYear == null) {
                viewModel.getWorkYear();
            } else {
                shoWorkYearPop(null);
            }
        }
    }

    public void showList(List<FindPeopleEntity> list, boolean isLoadMore, boolean isLast) {
        if (isLoadMore) {
            binding.recyclerView.loadMoreComplete();
            if (list != null && list.size() > 0) {
                findPeopleAdapter.addDatas(list);
            }
            binding.recyclerView.setNoMore(isLast);
        } else {
            binding.recyclerView.refreshComplete();
            if (list != null && list.size() > 0) {
                binding.emptyView.setVisibility(View.GONE);
                findPeopleAdapter.setDatas(list);
                binding.recyclerView.smoothScrollToPosition(0);
            } else {
                findPeopleAdapter.clearDatas();
                binding.emptyView.setVisibility(View.VISIBLE);
            }
        }
    }

    /**
     * 工种
     * @param data 工种的pop的列表数据
     */
    public void showWorkTypePop(List<PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity>>>>> data) {
        if (popWorkType == null) {
            popWorkType = new ThreeLevelFilterWindow(getActivity());
            popWorkType.setBgView(binding.grayLayout);
            if (mAddress != null){
                int position = 0;
                for (int i = 0; i < data.size(); i++) {
                    if (mAddress.getNo().equals(data.get(i).getNo())){
                        position = i;
                        break;
                    }
                }
                popWorkType.setDefaultItem(position+1);
            }
            popWorkType.setOnResultClick(new ThreeLevelFilterWindow.OnResultClick() {
                @Override
                public void result(Object key1, Object key2, Object key3) {
                    pickResultIndustry.setNo(((PickResultEntity) key1).getNo());
                    pickResultIndustry.setName(((PickResultEntity) key1).getName());
                    pickResultCategory.setNo(((PickResultEntity) key2).getNo());
                    pickResultCategory.setName(((PickResultEntity) key2).getName());
                    pickResultWorkType.setNo(((PickResultEntity) key3).getNo());
                    pickResultWorkType.setName(((PickResultEntity) key3).getName());
                    initFilterData(false);
                }

                @Override
                public void reset() {
                    pickResultIndustry.clear();
                    pickResultCategory.clear();
                    pickResultWorkType.clear();
                    initFilterData(false);
                }
            });
            popWorkType.setOnDismissListener(() -> {
                if (TextUtils.isEmpty(pickResultIndustry.getNo())) {
//                    setTextViewChange(binding.tvWorkType, R.mipmap.ic_down_arrow_n, R.color.color_666666);
                } else {
//                    setTextViewChange(binding.tvWorkType, R.mipmap.ic_down_arrow, R.color.color_main);
                }
            });

            if (!"".equals(pickResultIndustry.getNo())){
                popWorkType.setThreeLevelDataForNormal(data,pickResultIndustry.getNo(),pickResultCategory.getNo(),pickResultWorkType.getNo());
            }else {
                popWorkType.setThreeLevelData(data);
            }
        }
    }

    /**
     * 工人级别pop
     * @param data 工人级别pop列表数据
     */
    public void shoWorkerLevelPop(List<PickSelectEntity> data) {
        if (popWorkerLevel == null) {
            int position = 0;
            popWorkerLevel = new PickPopWindow1(this);
            popWorkerLevel.setBgView(binding.grayLayout);
            if (!"".equals(pickResultWorkerLevel.getNo())){
                for (int i = 0; i < data.size(); i++) {
                    if (data.get(i).getNo().equals(pickResultWorkerLevel.getNo())){
                        position = i;
                    }
                }
            }
            popWorkerLevel.setOnResultClick((PickPopWindow1.OnResultClick) (key1, key2, key3) -> {
                pickResultWorkerLevel.setNo((String) key1);
                pickResultWorkerLevel.setName((String) key2);
                pickResultWorkerLevel.setPosition((int) key3);
                initFilterData(false);
            });
            popWorkerLevel.setOnDismissListener(() -> {
            });

            popWorkerLevel.setData(data);
            popWorkerLevel.setDefaultItem(position);
        }
    }

    /**
     * 工龄pop
     * @param data 工龄pop列表数据
     */
    public void shoWorkYearPop(List<PickSelectEntity> data) {
        if (popWorkYear == null) {
            int position = 0;
            popWorkYear = new PickPopWindow1(this);
            popWorkYear.setBgView(binding.grayLayout);
            if (!"".equals(pickResultWorkYear.getNo())){
                for (int i = 0; i < data.size(); i++) {
                    if (data.get(i).getNo().equals(pickResultWorkYear.getNo())){
                        position = i;
                    }
                }
            }
            popWorkYear.setOnResultClick((PickPopWindow1.OnResultClick) (key1, key2, key3) -> {
                pickResultWorkYear.setNo((String) key1);
                pickResultWorkYear.setName((String) key2);
                pickResultWorkYear.setPosition((int) key3);
                initFilterData(false);
            });
            popWorkYear.setOnDismissListener(() -> {
            });

            popWorkYear.setData(data);
            popWorkYear.setDefaultItem(position);
        }
    }

    /**
     * 省市区pop
     * @param data 地区选择的列表数据
     */
    public void showCityPop(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> data) {
        if (popCity == null) {
            popCity = new ThreeLevelAreaWindow(getActivity()){
                @Override
                protected void provinceClick(BaseAreaEntity baseAreaEntity, int position) {
                    super.provinceClick(baseAreaEntity, position);
                    pickResultProvince.setNo(baseAreaEntity.getNo());
                    pickResultProvince.setName(baseAreaEntity.getSimpleName());
                    if (getString(R.string.app_all).equals(baseAreaEntity.getSimpleName()) || pickResultProvince.getName().equals(baseAreaEntity.getSimpleName())){
                        pickResultCity.setNo("");
                        pickResultCity.setName("");
                    }
                }

                @Override
                protected void cityClick(BaseAreaEntity baseAreaEntity, int position) {
                    super.cityClick(baseAreaEntity, position);
                    pickResultCity.setNo(baseAreaEntity.getNo());
                    pickResultCity.setName(baseAreaEntity.getSimpleName());
                    if (getString(R.string.app_all).equals(baseAreaEntity.getSimpleName()) || pickResultCity.getName().equals(baseAreaEntity.getSimpleName())){
                        pickResultArea.setNo("");
                        pickResultArea.setName("");
                    }
                }

                @Override
                protected void areaClick(BaseAreaEntity baseAreaEntity, int position) {
                    super.areaClick(baseAreaEntity, position);
                    pickResultArea.setNo(baseAreaEntity.getNo());
                    pickResultArea.setName(baseAreaEntity.getSimpleName());
                }
            };
            popCity.setBgView(binding.grayLayout);
            popCity.setShowSimpleName(true);
            popCity.setProvinceSingle();
            popCity.setCitySingle();
            popCity.setAreaSingle();
            if ("".equals(pickResultProvince.getNo())){
//                pickResultProvince.setName(viewModel.getAreaName());
            }
            popCity.setOnResultClick((key1, key2, key3) -> {
                initFilterData(false);
            });
            popCity.setOnDismissListener(() -> {
            });

            if (!"".equals(pickResultProvince.getNo())){
                popCity.setDataForTeamMainPage(data,true,pickResultProvince.getNo(),pickResultCity.getNo());
            }
        }

    }
}